/**
 * 资源信息维护页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("AUTHORITY_RESOURCE_INFO", "AUTHORITY");

    $("#resourceType").change(function(){
        changeResourceType($(this).val());
    });

    $("#save").click(function(){
        save();
    });

    $("#goBack").click(function(){
        window.history.back();
    });

    init();
});

/**
 * 变更资源类型
 * @param val
 */
function changeResourceType(val){
    $('#editForm').validator("cleanUp");
    if(val == 1){
        $("#resourceIconDiv").show();
        $("#resourceUrlDiv").hide();

        $("#resourceUrl").val("");
        $("#resourceUrl").attr("disabled", true);
    }
    if(val == 2){
        $("#resourceIconDiv").show();
        $("#resourceUrlDiv").show();

        $("#resourceUrl").attr("disabled", false);
    }
    if(val == ""){
        $("#resourceIconDiv").hide();
        $("#resourceUrlDiv").hide();
    }
}

/**
 * 初始化资源数据
 */
function init(){
    jQuery.ajax({
        type: "POST",
        dataType: "json",
        url: springUrl+"/web/resourceInfo/selectByPrimaryKey",
        data: {
            id: $('#id').val()
        },
        success: function (data) {
            $("#parentResourceCode").val(data.parentResourceCode);
            $("#resourceCode").val(data.resourceCode);
            $("#resourceName").val(data.resourceName);
            changeResourceType(data.resourceType);
            $("#resourceType").select2("val", data.resourceType);
            $("#resourceIcon").val(data.resourceIcon);
            $("#resourceUrl").val(data.resourceUrl);
            $("#resourceSort").val(data.resourceSort);
            $("#remark").val(data.remark);
        },
        error: function(data){
            Commons.showError("初始化资源数据失败!");
        }
    });
}

/**
 * 保存
 */
function save(){

    if($('#editForm').trigger("validate").isValid()){
        jQuery.ajax({
            type: "POST",
            dataType: "json",
            url: springUrl+"/web/resourceInfo/updateByPrimaryKey",
            data: {
                id: $('#id').val(),
                parentResourceCode: $('#parentResourceCode').val(),
                resourceCode: $('#resourceCode').val(),
                resourceName: $('#resourceName').val(),
                resourceType: $('#resourceType').val(),
                resourceUrl: $('#resourceUrl').val(),
                resourceIcon: $('#resourceIcon').val() != "" ? $('#resourceIcon').val() : "leaf",
                resourceSort: $('#resourceSort').val(),
                remark: $('#remark').val()
            },
            success: function (data) {
                Commons.showSuccess("数据保存成功!", function(){
                    window.location.href = "/web/resourceInfo/index";
                });
            },
            error: function(data){
                Commons.showError("数据保存失败!");
            }
        });
    }
}

